package com.baidu.speech.messageactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.aip.asrwakeup3.core.mini.ActivityMiniRecog;
import com.baidu.speech.asr.SpeechConstant;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends ActivityMiniRecog {
    private static final String TAG = "MainActivity";
    private WebView webview;
    private Button btnRecord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        webview = findViewById(R.id.my_webview);
        btnRecord = findViewById(R.id.btn_sound_input);
        webview.loadUrl("http://106.12.30.77:8082/suggestions.html");
        WebSettings webSettings = webview.getSettings();
        webview.addJavascriptInterface(this, "nativeMethod");
        webSettings.setJavaScriptEnabled(true);

        btnRecord.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    MainActivity.this.start();
                }
                if(motionEvent.getAction() == MotionEvent.ACTION_UP){
                    MainActivity.this.stop();
                }
                return false;
            }
        });
    }
    @JavascriptInterface
    public void toActivity() {
        if(btnRecord.getVisibility()==View.GONE) {
            btnRecord.setVisibility(View.VISIBLE);
        }else{
            btnRecord.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String message = data.getStringExtra("data");
        Log.e(TAG, "onActivityResult: "+ message);
        webview.loadUrl("javascript:send('" + message + "')");

    }


    @Override
    public void onEvent(String name, String params, byte[] data, int offset, int length) {
        String logTxt = "name: " + name;
        if (params != null && !params.isEmpty()) {
            logTxt += " ;params :" + params;
        }
        if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)) {
            if (params != null && params.contains("\"nlu_result\"")) {
                if (length > 0 && data.length > 0) {
                    logTxt += ", 语义解析结果：" + new String(data, offset, length);
                }
            }
        } else if (data != null) {
            logTxt += " ;data length=" + data.length;
        }

        Log.e(TAG, "onEvent: "+logTxt );
        if(name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)){
            JSONObject jsonObject;
            try {
                jsonObject = new JSONObject(params);
                String result_type = jsonObject.getString("result_type");
                if(result_type.equals("final_result")){
                    String bestResult = jsonObject.getString("best_result");
                    Log.e(TAG, "onEvent: "+bestResult);
                    if(!bestResult.isEmpty()) {
                        webview.loadUrl("javascript:send('" + bestResult + "')");
                    }else{

                    }
                    Log.e(TAG, "onEvent: "+ bestResult);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}
